//
//  GGNetWorkCache.m
//  TestAppDelegateService
//
//  Created by yuemei on 2017/9/25.
//  Copyright © 2017年 yuemei. All rights reserved.
//

#import "GGNetWorkCache.h"
#import "GGNetWorkHelper.h"
#import <YYKit/YYKit.h>

static NSString *const GGNetworkResponseCache = @"GGNetworkResponseCache";

@implementation GGNetWorkCache

static YYCache *_dataCache;

+ (void)initialize {
    _dataCache = [YYCache cacheWithName:GGNetworkResponseCache];
}

+ (void)setNetWorkCacheWithData:(id)data URL:(NSString *)url parameters:(id)parameters{
    NSString *cacheKey = [GGNetWorkHelper cacheKeyWithURL:url parameters:parameters];
    [_dataCache setObject:data forKey:cacheKey];
}

+ (void)setNetWorkCacheTimeWithURL:(NSString *)url parameters:(id)parameters{
    NSString *cacheTimeKey = [NSString stringWithFormat:@"%@CacheTime",[GGNetWorkHelper cacheKeyWithURL:url parameters:parameters]];
//    NSTimeInterval interval = [[NSDate date] timeIntervalSince1970] * 1000;
    NSDate *date = [NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate: date];
    NSDate *localeDate = [date  dateByAddingTimeInterval: interval];
    [_dataCache setObject:localeDate forKey:cacheTimeKey];
}

+ (id)netWorkCacheForURL:(NSString *)url parameters:(id)parameters{
    NSString *cacheKey = [GGNetWorkHelper cacheKeyWithURL:url parameters:parameters];
    return [_dataCache objectForKey:cacheKey];
}


+ (NSDate *)netWorkCacheTimeForURL:(NSString *)url parameters:(id)parameters{
    NSString *cacheTimeKey = [NSString stringWithFormat:@"%@CacheTime",[GGNetWorkHelper cacheKeyWithURL:url parameters:parameters]];
    id time = [_dataCache objectForKey:cacheTimeKey];
    return  (NSDate *)time;
}

+ (NSInteger)getAllNetWorkCacheSize{
    return [_dataCache.diskCache totalCost];
}

+ (void)removeAllNetWorkCache{
    [_dataCache.diskCache removeAllObjects];
}



@end
